const DataTypes = require('sequelize').DataTypes;
const _m_autocode_rules = require('./m_autocode_rules');
const _m_config = require('./m_config');
const _m_consumers = require('./m_consumers');
const _m_ip = require('./m_ip');
const _m_regions = require('./m_regions');
const _m_roles = require('./m_roles');
const _m_shops = require('./m_shops');
const _m_storage = require('./m_storage');
const _m_tenant_fees = require('./m_tenant_fees');
const _m_tenants = require('./m_tenants');
const _m_users = require('./m_users');
const _m_wechat_user = require('./m_wechat_user');
const _sum_num = require('./sum_num');
const _t_autocodes = require('./t_autocodes');
const _t_customers = require('./t_customers');
const _t_deliveries = require('./t_deliveries');
const _t_delivery_details = require('./t_delivery_details');
const _t_devices = require('./t_devices');
const _t_material_catalog = require('./t_material_catalog');
const _t_materials = require('./t_materials');
const _t_message_logs = require('./t_message_logs');
const _t_operation_logs = require('./t_operation_logs');
const _t_sale_details = require('./t_sale_details');
const _t_sales = require('./t_sales');

function initModels (sequelize) {
  const m_autocode_rules = _m_autocode_rules(sequelize, DataTypes);
  const m_config = _m_config(sequelize, DataTypes);
  const m_consumers = _m_consumers(sequelize, DataTypes);
  const m_ip = _m_ip(sequelize, DataTypes);
  const m_regions = _m_regions(sequelize, DataTypes);
  const m_roles = _m_roles(sequelize, DataTypes);
  const m_shops = _m_shops(sequelize, DataTypes);
  const m_storage = _m_storage(sequelize, DataTypes);
  const m_tenant_fees = _m_tenant_fees(sequelize, DataTypes);
  const m_tenants = _m_tenants(sequelize, DataTypes);
  const m_users = _m_users(sequelize, DataTypes);
  const m_wechat_user = _m_wechat_user(sequelize, DataTypes);
  const sum_num = _sum_num(sequelize, DataTypes);
  const t_autocodes = _t_autocodes(sequelize, DataTypes);
  const t_customers = _t_customers(sequelize, DataTypes);
  const t_deliveries = _t_deliveries(sequelize, DataTypes);
  const t_delivery_details = _t_delivery_details(sequelize, DataTypes);
  const t_devices = _t_devices(sequelize, DataTypes);
  const t_material_catalog = _t_material_catalog(sequelize, DataTypes);
  const t_materials = _t_materials(sequelize, DataTypes);
  const t_message_logs = _t_message_logs(sequelize, DataTypes);
  const t_operation_logs = _t_operation_logs(sequelize, DataTypes);
  const t_sale_details = _t_sale_details(sequelize, DataTypes);
  const t_sales = _t_sales(sequelize, DataTypes);

  // 管理角色与管理用户的关联关系
  m_roles.hasMany(m_users, { sourceKey: 'hash', foreignKey: 'hash' });
  m_users.belongsTo(m_roles, { targetKey: 'hash', foreignKey: 'hash' });

  // 系统配置表与租户的关系
  m_tenants.hasMany(m_config, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_config.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 自动编码规则表与租户的关系
  m_tenants.hasMany(m_autocode_rules, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_autocode_rules.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 租户会员费与租户的关系
  m_tenants.hasMany(m_tenant_fees, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_tenant_fees.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 微信会员关系
  m_users.hasOne(m_wechat_user, { sourceKey: 'id', foreignKey: 'user_id' });
  m_wechat_user.belongsTo(m_users, { targetKey: 'id', foreignKey: 'user_id' });

  // 租户与租户用户的关系
  m_tenants.hasMany(m_users, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_users.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 租户用户角色的关系
  m_tenants.hasMany(m_roles, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_roles.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  m_roles.hasMany(m_users, { sourceKey: 'tenant_hash', foreignKey: 'tenant_hash' });
  m_users.belongsTo(m_roles, { targetKey: 'tenant_hash', foreignKey: 'tenant_hash' });

  // 租户消息日志的关系
  m_tenants.hasMany(t_message_logs, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_message_logs.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  m_users.hasMany(t_message_logs, { sourceKey: 'id', foreignKey: 'sender' });
  t_message_logs.belongsTo(m_users, { targetKey: 'id', foreignKey: 'sender' });

  // 租户操作日志的关系
  m_tenants.hasMany(t_operation_logs, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_operation_logs.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  m_users.hasMany(t_operation_logs, { sourceKey: 'id', foreignKey: 'user_id' });
  t_operation_logs.belongsTo(m_users, { targetKey: 'id', foreignKey: 'user_id' });

  // 租户自动编码记录的关系
  m_tenants.hasMany(t_autocodes, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_autocodes.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 租户店铺的关系
  m_tenants.hasMany(m_shops, { sourceKey: 'id', foreignKey: 'tenant_id' });
  m_shops.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 租户物料分类的关系
  m_tenants.hasMany(t_material_catalog, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_material_catalog.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  t_material_catalog.hasOne(t_material_catalog, { sourceKey: 'id', foreignKey: 'parent_id' });

  // 租户物料的关系
  m_tenants.hasMany(t_materials, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_materials.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  t_material_catalog.hasMany(t_materials, { sourceKey: 'id', foreignKey: 'catalog_id1' });
  t_materials.belongsTo(t_material_catalog, { targetKey: 'id', foreignKey: 'catalog_id1', as: 'catalog1' });
  t_material_catalog.hasMany(t_materials, { sourceKey: 'id', foreignKey: 'catalog_id2' });
  t_materials.belongsTo(t_material_catalog, { targetKey: 'id', foreignKey: 'catalog_id2', as: 'catalog2' });
  t_material_catalog.hasMany(t_materials, { sourceKey: 'id', foreignKey: 'catalog_id3' });
  t_materials.belongsTo(t_material_catalog, { targetKey: 'id', foreignKey: 'catalog_id3', as: 'catalog3' });
  t_material_catalog.hasMany(t_materials, { sourceKey: 'id', foreignKey: 'catalog_id4' });
  t_materials.belongsTo(t_material_catalog, { targetKey: 'id', foreignKey: 'catalog_id4', as: 'catalog4' });

  // 租户客户的关系
  m_tenants.hasMany(t_customers, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_customers.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  // 租户销售单的关系
  m_tenants.hasMany(t_sales, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_sales.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  t_sales.belongsTo(t_customers, { targetKey: 'uid', foreignKey: 'customer_uid' });
  t_sales.belongsTo(m_consumers, { targetKey: 'uid', foreignKey: 'consumer_uid' });

  m_tenants.hasMany(t_sale_details, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_sale_details.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  t_sales.hasMany(t_sale_details, { sourceKey: 'uid', foreignKey: 'master_uid' });
  t_sale_details.belongsTo(t_sales, { targetKey: 'uid', foreignKey: 'master_uid' });
  t_sale_details.belongsTo(t_materials, { targetKey: 'uid', foreignKey: 'material_uid' });
  t_sales.belongsTo(m_shops, { targetKey: 'tenant_id', foreignKey: 'tenant_id' });

  // 租户送货单的关系
  m_tenants.hasMany(t_deliveries, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_deliveries.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });

  m_tenants.hasMany(t_delivery_details, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_delivery_details.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  t_deliveries.hasMany(t_delivery_details, { sourceKey: 'uid', foreignKey: 'master_uid' });
  t_sale_details.hasMany(t_delivery_details, { sourceKey: 'uid', foreignKey: 'sale_detail_uid' });
  t_delivery_details.belongsTo(t_sale_details, { targetKey: 'uid', foreignKey: 'sale_detail_uid' });
  t_delivery_details.belongsTo(t_deliveries, { targetKey: 'uid', foreignKey: 'master_uid' });

  // 租户设备的关系
  m_tenants.hasMany(t_devices, { sourceKey: 'id', foreignKey: 'tenant_id' });
  t_devices.belongsTo(m_tenants, { targetKey: 'id', foreignKey: 'tenant_id' });
  // 设备与客户的关系
  t_customers.hasMany(t_devices, { sourceKey: 'uid', foreignKey: 'customer_uid' });
  t_devices.belongsTo(t_customers, { targetKey: 'uid', foreignKey: 'customer_uid' });

  return {
    m_autocode_rules,
    m_config,
    m_consumers,
    m_ip,
    m_regions,
    m_roles,
    m_shops,
    m_storage,
    m_tenant_fees,
    m_tenants,
    m_users,
    m_wechat_user,
    sum_num,
    t_autocodes,
    t_customers,
    t_deliveries,
    t_delivery_details,
    t_devices,
    t_material_catalog,
    t_materials,
    t_message_logs,
    t_operation_logs,
    t_sale_details,
    t_sales
  };
}
module.exports = initModels;
module.exports.initModels = initModels;
module.exports.default = initModels;
